<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property int $id
 * @property string $email
 * @property string $password
 * @property string $name
 *
 * The followings are the available model relations:
 * @property Item[] $items
 * @property Comment[] $comments
 */
class User extends CActiveRecord {
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		return array(
			array('email, password, name', 'required'),
			array('email, name, description', 'filter', 'filter' => 'trim'),
			array('name', 'length', 'max' => 64),
			array('description', 'length', 'max' => 512),
			array('email', 'length', 'max' => 254),
			array('email', 'email'),
			array('email', 'unique'),
			// The following rule is used by search().
			array('id, email, name', 'safe', 'on' => 'search'),
		);
	}

	/**
	 * Changes the old password to the new one after validation.
	 * The new password should be validated by ChangePasswordForm.
	 * Use this method to update the password instead of directly updating it.
	 * @param string $newPassword the new password.
	 * @return boolean whether the password was chaged successfully.
	 */
	public function changePassword($newPassword) {
		$backup = $this->password;
		$this->password = self::hashPassword($newPassword);
		if($this->validate(array('password'))) {
			return $this->update(array('password'));
		} else {
			$this->password = $backup;
			return false;
		}
	}

	/**
	 * Hashes the password, usaully to deal with the database.
	 * @param string $string the password to hash.
	 * @return string the hashed password.
	 */
	public static function hashPassword($string) {
		return sha1($string);
	}

	/**
	 * Generates a random password for the user and saves it to the database.
	 * @return string the new password before hashing.
	 */
	public function resetPassword() {
		$password = substr(md5(time()), 0, 8); //don't want it to be too long
		$this->password = self::hashPassword($password);
		$this->update(array('password'));

		return $password;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'user';
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		return array(
			'items' => array(self::HAS_MANY, 'Item', 'user_id'),
			'comments' => array(self::HAS_MANY, 'Comment', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'User',
			'email' => 'Email',
			'password' => 'Password',
			'name' => 'Name',
			'description' => 'Description',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id, true);
		$criteria->compare('email', $this->email, true);
		$criteria->compare('name', $this->name, true);

		return new CActiveDataProvider($this, array(
				'criteria' => $criteria,
			));
	}

	public function __toString() {
		return $this->id;
	}

	protected function beforeSave() {
		if($this->getIsNewRecord()) {
			$this->password = self::hashPassword($this->password);
		}
		return parent::beforeSave();
	}

}